package jp.co.hcs.attendancemanagement.dialog.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jp.co.hcs.attendancemanagement.AMConstants;
import jp.co.hcs.attendancemanagement.action.AMAction;
import jp.co.hcs.attendancemanagement.dialog.logic.RestTimeDetailLogic;
import jp.co.hcs.attendancemanagement.inputdailyattendance.actionform.InputDailyAttendanceForm;
import jp.co.hcs.attendancemanagement.inputdailyattendance.logic.dto.RestTimeDTO;
import jp.co.hcs.attendancemanagement.util.StringUtil;
import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class RestDetailAction extends AMAction {

  public ActionForward executeImpl(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    String mode = request.getParameter("mode");

    String userId = (String) request.getSession().getAttribute(AMConstants.USER_LOGGED_IN);
    InputDailyAttendanceForm inputDailyAttendanceForm = (InputDailyAttendanceForm) form;
    inputDailyAttendanceForm.setUserId(userId);

    RestTimeDetailLogic logic = new RestTimeDetailLogic();

    if ("load".equals(mode)) {
      String year = request.getParameter("year");
      String month = request.getParameter("month");
      String day = request.getParameter("day");
      boolean isDateChanged = Boolean.valueOf(request.getParameter("datechange"));
        
      logic.loadRestDetail(inputDailyAttendanceForm, isDateChanged, year, month, day);
      List<RestTimeDTO> listRestDetails = inputDailyAttendanceForm.getRestTimeInfoDTOList();

      JSONArray jsonArray = JSONArray.fromObject(listRestDetails);

      PrintWriter pw = response.getWriter();
      pw.write("{\"listRestDetails\":" + jsonArray.toString() + "}");
      pw.flush();
      pw.close();
    } else if ("save".equals(mode)) {
      String[] startTimeList = request.getParameterValues("restStartTime");
      String[] endTimeList = request.getParameterValues("restEndTime");
      String[] restTimeList = request.getParameterValues("restTime");
      String[] commentList = request.getParameterValues("comment");

      if (startTimeList != null && startTimeList.length > 0) {
        List<RestTimeDTO> restList = new ArrayList<RestTimeDTO>();
        RestTimeDTO restDto;
        int index = 0;
        for (int i = 0; i < startTimeList.length; i++) {
          if (!StringUtil.isEmpty(startTimeList[i]) && !StringUtil.isEmpty(startTimeList[i])) {
            restDto = new RestTimeDTO();
            restDto.setTakingRestNum(++index);
            restDto.setRestStartTime(startTimeList[i]);
            restDto.setRestEndTime(endTimeList[i]);
            restDto.setRestTime(restTimeList[i]);
            restDto.setComment(commentList[i]);
            restList.add(restDto);
          }
        }
        inputDailyAttendanceForm.setRestTimeInfoDTOList(restList);
      }

      String totalRestTime = request.getParameter("totalRestTime");
      if (!StringUtil.isEmpty(totalRestTime)) {
        inputDailyAttendanceForm.getTimeDTO().setRestTime(totalRestTime);
      }

      PrintWriter pw = response.getWriter();
      pw.write("success");
      pw.flush();
      pw.close();
    }

    return null;
  }

}
